var listPage = avalon.define({
	$id: "listPage",
	noData: "",
	channel:0,
	policys: []
});

mui.ready(function() {

	var pageNo = 1;
	var mui_policy = mui('#policy-list');
	var channel = $.getParameter("channel");
	if(null!=channel){
		listPage.channel=channel;
	}

	
	mui.init({
		pullRefresh: {
			container: "#policy-list",
			down: {
				height: 50,
				auto: true,
				contentdown: $.i18n.prop('Pull_down_to_refresh'),
				contentover: $.i18n.prop('Release_to_immediate_refresh'),
				contentrefresh: $.i18n.prop('Loading'),
				callback: function() {
					listPage.noData = "";
					setTimeout(function() {
						pageNo = 1;
						mui.ajax('/channelpolicy/list.json', {
							data: {
								pageNo: pageNo
							},
							dataType: 'json',
							success: function(data) {
								listPage.policys = data;
								mui_policy.pullRefresh().endPulldownToRefresh();
								
								if(listPage.policys.length == 0) {
									listPage.noData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
								}
							}
						});
					}, window.DAHUA_CONFIG.LOADING_TIME);

				}
			},
			up: {
				height: 50,
				auto: false,
				contentrefresh: $.i18n.prop('Loading'),
				contentnomore: $.i18n.prop('noData'),
				callback: function() {
					listPage.noData="";
					setTimeout(function() {
						pageNo = pageNo + 1;
						mui.ajax('/channelpolicy/list.json', {
							data: {
								pageNo: pageNo
							},
							dataType: 'json',
							success: function(items) {
								var policys = listPage.policys;
								avalon.each(items, function(i, item) {
									policys.push(item);
								});
								listPage.policys = policys;
								mui_policy.pullRefresh().endPullupToRefresh(false);
								if(listPage.policys.length==0){
									listPage.noData="<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
								}
							}
						});
					}, window.DAHUA_CONFIG.LOADING_TIME);

				}
			}
		}
	});
	
	mui(".mui-table-view").on('tap', 'li', function() {
		var cid = this.getAttribute('data-id');
		var url = "pdp.html";
		mui.openWindow({
			url: url + "?cid=" + cid
		});
	});

	mui('.header').on('tap', '.dahua-action-back', function(e) {
		if(0==listPage.channel){
			window.history.go(-1);
		}else{
			var url = this.getAttribute('href');
			mui.openWindow({
				url: url
			});
		}
	});

});